package com.vm.adapter;

import java.util.List;

import com.vm.broadcast.AppInstallReceiver;
import com.vm.db.OperationDao;
import com.vm.db.UpgradeSoftDao;
import com.vm.model.UpgradeSoft;
import com.wwtscapp.apk.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年2月2日 上午10:32:21]
 * @Version: [v1.0]
 * 
 */
@SuppressLint("InflateParams")
public class UninstallAdapter extends BaseAdapter {

	private Context mContext;
	private List<UpgradeSoft> listSofts;
	boolean isFirstZero = true;

	/**
	 * 
	 */
	public UninstallAdapter(Context context, List<UpgradeSoft> lists) {
		this.mContext = context;
		this.listSofts = lists;

	}

	@Override
	public int getCount() {
		return listSofts.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.software_uninstall_item, null);
			viewHolder = new ViewHolder();

			/**
			 * 获取各个需要显示属性的控件
			 */
			viewHolder.tv_softName = (TextView) convertView
					.findViewById(R.id.tv_softName);
			viewHolder.iv_softImage = (ImageView) convertView
					.findViewById(R.id.iv_softImage);
			viewHolder.tv_softSize = (TextView) convertView
					.findViewById(R.id.tv_softSize);
			viewHolder.tv_now_version_number = (TextView) convertView
					.findViewById(R.id.tv_now_version_number);
			viewHolder.btn_uninstall = (Button) convertView
					.findViewById(R.id.btn_uninstall);
			viewHolder.btn_uninstall.setOnClickListener(btn_uninstallListener);
			// viewHolder.rb_star = (RatingBar) convertView
			// .findViewById(R.id.rb_star);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (position == 0) {
			if (isFirstZero) {
				isFirstZero = false;
			} else {
				return convertView;
			}

		} else {
			isFirstZero = true;
		}

		UpgradeSoft appInfo = listSofts.get(position);

		viewHolder.tv_softName.setTag(appInfo.getName());
		viewHolder.btn_uninstall.setTag(appInfo);
		viewHolder.tv_softSize.setText(Float.toString(appInfo.getSize()));
		// 名称

		if (appInfo.getName() != null) {
			// String softName = StringUtil.getShotName(soft.getName());
			viewHolder.tv_softName.setText(appInfo.getName());
		}

		if (appInfo.getCurrentVersion() != null) {
			viewHolder.tv_now_version_number.setText(appInfo
					.getCurrentVersion());
		}

		if (appInfo.getPackageName() != null) {
			try {

				Drawable drawable = mContext.getPackageManager()
						.getApplicationIcon(appInfo.getPackageName());
				if (drawable != null) {
					viewHolder.iv_softImage.setImageDrawable(drawable);
				}

			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block

				// 从本地数据库中删除该应用
				UpgradeSoftDao dao = new UpgradeSoftDao();
				dao.delete(appInfo.getPackageName());
				e.printStackTrace();

				// viewHolder.iv_softImage.setImageDrawable(mContext
				// .getResources().getDrawable(R.drawable.error));
				// //android.content.pm.PackageManager$NameNotFoundException
				// 系统无法根据包名找到图片,该应用在设备上已卸载,但在本地数据库中并没有删除

			}
		}

		return convertView;
	}

	OnClickListener btn_uninstallListener = new OnClickListener() {

		@SuppressLint("HandlerLeak")
		@Override
		public void onClick(View v) {
			// String packageName = v.getTag().toString();
			final UpgradeSoft appInfo = (UpgradeSoft) v.getTag();
			Uri packageURI = Uri.parse("package:" + appInfo.getPackageName());

			// 执行本地数据库删除

			final int size = listSofts.size();
			// 直接将应用从List中移除

			final OperationDao op = new OperationDao();
			op.insert(appInfo.getSoftId(), appInfo.getPackageName());

			Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
					packageURI);
			v.getContext().startActivity(uninstallIntent);
			/**
			 * 接收消息
			 */
			Handler mainHandler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					switch (msg.what) {

					/**
					 * 在本地数据库中删除该应用
					 */
					case 200:
						if (size > 0) {
							UpgradeSoftDao dao = new UpgradeSoftDao();
							dao.delete(appInfo.getPackageName());

							op.deleteOne(appInfo.getPackageName());
							listSofts.remove(appInfo);
							notifyDataSetChanged();

						}
						break;

					default:
						break;
					}
					super.handleMessage(msg);
				}
			};

			AppInstallReceiver.mainHandler = mainHandler;

		}
	};

	static class ViewHolder {
		TextView tv_softName;
		ImageView iv_softImage;
		TextView tv_softSize;
		TextView tv_now_version_number;
		Button btn_uninstall;
		// RatingBar rb_star;
	}

}
